package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.EquipmentType;
import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.EquipmentTypeService;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.util.Collections;
import java.util.List;

/**
 * ViewHelper for EquipmentType objects
 *
 * @author TSgt Jason Ferguson
 */
@Service("equipmentTypeViewHelper")
public class EquipmentTypeViewHelper implements PageContextAwareBean {

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    private EquipmentTypeService typeService;

    @Autowired
    public void setTypeService(EquipmentTypeService typeService) {
        this.typeService = typeService;
    }

    private EquipmentItemService equipmentItemService;

    @Autowired
    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    public List<EquipmentType> getAll() {

        List<EquipmentType> typeList = typeService.getAll();
        Collections.sort(typeList);
        return typeList;
    }

    public EquipmentType getEquipmentType() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int itemId = Integer.parseInt(httpServletRequest.getParameter("itemId"));

        EquipmentItem item = equipmentItemService.getById(itemId);

        if (item.getItemModel() != null) {
            return item.getItemModel().getType();
        }

        return null;

    }
}
